package midend;

import midend.IR_Type.IR_Type;
import midend.IR_Value.IR_Value;

import java.util.ArrayList;

public class User{
    private ArrayList<IR_Value> valueList;
    public User() {
        valueList = new ArrayList<>();
    }

    public void addValue(IR_Value value) {
        valueList.add(value);
        if (value != null) {
            value.addUse(new Use(value, this));
        }

    }

    public IR_Value getUserValue(int count) {
        return valueList.get(count);
    }

    public ArrayList<IR_Value> getUserValueList() {
        return this.valueList;
    }

    public void setValue(int pos, IR_Value value) {
        if (pos >= valueList.size()) {
            return;
        }
        IR_Value before = valueList.get(pos);
        if (before != null && before != value) {
            before.removeUserFromUse(this);
        }
        this.valueList.set(pos, value);
        if (value != null && before != value) {
            value.addUse(new Use(value, this));
        }
    }

    public void removeUseOfValue() {
        if (valueList.isEmpty()) {
            return;
        }
        for (IR_Value value : valueList) {
            if (value != null) {
                value.removeUserFromUse(this);
            }
        }
    }

    public void updateop(int index, IR_Value newValue) {
        return;
    }
}
